<?php

function unsDepartement_addTutorialGroups($name, $formationID) {
    global $babDB;
    global $babBody;

    if (null != $name && null != $formationID) {

        $formation = unsDepartement_getFormation($formationID);
        $groupID = bab_createGroup($name, " ", $formation['coordinatorID'], $formation['groupID']); // on crée le groupe de la formation

        if (0 != $groupID) {// Si != 0 alors le groupe a été crée et ça retourne l'id du group
            $babDB->db_query("INSERT INTO " . UNSDEPARTEMENT_TUTORIAL_GROUPS_TABLE_NAME . " VALUES (null, '" . $name . "', " . $formationID . ", " . $groupID . ", now(), " . $GLOBALS['BAB_SESS_USERID'] . ");");
        } else {
            $babBody->addError(bab_translate("A Tutorial Group with this name all ready exist !", $GLOBALS['babAddonFolder']));
        }
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
}

function unsDepartement_editTutorialGroup($id, $name)
{
    global $babDB, $babBody;

    $tutorialGroup = unsDepartement_getTutorialGroup($id);
    $groupID = $tutorialGroup['groupID'];
    bab_updateGroup($groupID, $name, $description);
    $babDB->db_query("update " . UNSDEPARTEMENT_TUTORIAL_GROUPS_TABLE_NAME . " set tutorialGroup_name ='" . $name . "' where id = " . $id);

}
        
function unsDepartement_getTutorialGroupsForFormation($id) {
    global $babDB;


    $res = $babDB->db_query("select * from " . UNSDEPARTEMENT_TUTORIAL_GROUPS_TABLE_NAME . " where tutorialGroup_formationID = " . $id);

    $tutorialGroups = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $tutorialGroups[$i] = $arr;
        $i++;
    }
    return $tutorialGroups;
}

function unsDepartement_getTutorialGroup($id) {
    global $babDB;
    
    $res = $babDB->db_query("select id, tutorialGroup_name as name, tutorialGroup_groupID as groupID, tutorialGroup_formationID as formationID from " . UNSDEPARTEMENT_TUTORIAL_GROUPS_TABLE_NAME . " where id = " . $id);

    $tutorialGroup = $babDB->db_fetch_array($res);

    return $tutorialGroup;
}

function unsDepartement_deleteTutorialGroup($id) 
{
    global $babDB;
    
    $listWorkPraticeGroup = unsDepartement_getWorkPraticeGroupsForTutorialGroup($id);
    $listWorkPraticeGroupLength = count($listWorkPraticeGroup);
    
    static $k = 0;
    while ($k < $listWorkPraticeGroupLength) {
        unsDepartement_deleteWorkPraticeGroup($listWorkPraticeGroup[$k]['id']);
        $k++;
    }
    
    
    
    $idTutorialGroup = $id;
    $tutorialGroup = unsDepartement_getTutorialGroup($idTutorialGroup);
    bab_removeGroup($tutorialGroup['groupID']);
    $babDB->db_query("delete from " . UNSDEPARTEMENT_TUTORIAL_GROUPS_TABLE_NAME . " where id = " . $idTutorialGroup . ";");
    //$babBody->addError(bab_translate("Le departement a bien ete supprime", $GLOBALS['babAddonFolder']));
    //$babBody->title = bab_translate("Departement List", $GLOBALS['babAddonFolder']);
    //displayListDepartement();
} 
?>
